
Namespace Logging

  ''' <summary>
  ''' Error logging class
  ''' </summary>
  ''' <remarks></remarks>
  Public NotInheritable Class LoggingHelper
    ''' <summary>
    ''' Logs messages. Adds the current date/time to the beginning of the message.
    ''' </summary>
    ''' <param name="message">Message text to log</param>
    ''' <remarks></remarks>
    Public Shared Sub Log(ByVal message As String)
      WriteToLog(Now.ToString(CultureInfo.CurrentCulture) + Microsoft.VisualBasic.Constants.vbNewLine + message, Nothing)
    End Sub

    ''' <summary>
    '''  Logs messages. Adds the current date/time to the beginning of the message.
    ''' </summary>
    ''' <param name="message">Message text to log</param>
    ''' <param name="severity">Message severity</param>
    ''' <remarks></remarks>
    Public Shared Sub Log(ByVal message As String, ByVal severity As System.Diagnostics.TraceEventType)
      WriteToLog(Now.ToString(CultureInfo.CurrentCulture) + Microsoft.VisualBasic.Constants.vbNewLine + message, severity)
    End Sub

    ''' <summary>
    ''' Logs exception.
    ''' </summary>
    ''' <param name="ex">Exception to log</param>
    ''' <remarks></remarks>
    Public Shared Sub Log(ByVal ex As System.Exception)
      WriteToLog(ex, Nothing, String.Empty)
    End Sub

    ''' <summary>
    '''  Logs messages. Adds the current date/time to the beginning of the message.
    ''' </summary>
    ''' <param name="ex">Exception to log</param>
    ''' <param name="severity">Message severity</param>
    ''' <param name="message">Message text to log</param>
    ''' <remarks></remarks>
    Public Shared Sub Log(ByVal ex As System.Exception, ByVal severity As System.Diagnostics.TraceEventType, ByVal message As String)
      WriteToLog(ex, severity, Now.ToString(CultureInfo.CurrentCulture) + Microsoft.VisualBasic.vbNewLine + message)
    End Sub

    Private Shared Sub WriteToLog(ByVal message As String, ByVal severity As System.Diagnostics.TraceEventType)

      If ApplicationHelper.IsAspNet() Then
        Dim log As New Microsoft.VisualBasic.Logging.AspLog
        log.WriteEntry(message, severity)
      Else
        My.Application.Log.WriteEntry(message, severity)
      End If


    End Sub

    Private Shared Sub WriteToLog(ByVal ex As System.Exception, ByVal severity As System.Diagnostics.TraceEventType, ByVal message As String)

      If ApplicationHelper.IsAspNet() Then
        Dim log As New Microsoft.VisualBasic.Logging.AspLog
        log.WriteException(ex, severity, message)
      Else
        My.Application.Log.WriteException(ex, severity, message)
      End If

    End Sub

    Private Sub New()
    End Sub

  End Class

End Namespace